#include <parser/front.h>
#include "lexer.h"

Front::Front() {
  this->trace_parsing = false;
  this->trace_scanning = false;
}

int Front::parse(const std::string & file_path) {
  this->file = file_path;
  loc.initialize(&this->file);
  scan_begin();
  yy::Parser parse(*this);
  parse.set_debug_level(trace_parsing);
  int res = parse();
  scan_end();
  return res;
}

void Front::scan_begin() {
  yyset_debug(trace_scanning);
  if(this->file.empty() || this->file == "-")
    yyin = stdin;
  else if(!(yyin = fopen(this->file.c_str(), "r"))) {
    std::cerr << "cannot open " << this->file << " : " << strerror(errno) << std::endl;
    exit(EXIT_FAILURE);
  }
}

void Front::scan_end() {
  fclose(yyin);
}
